﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenPop.Pop3;
using OpenPop.Mime;
using System.Data;
using System.Threading;
using System.Globalization;

public partial class ManagerDefault : System.Web.UI.Page
{
    public string cid;

    mypopclient pop3Client;
    int newmails = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        pop3Client = new mypopclient();
        pop3Client.Connect();
        if (!Page.IsPostBack)
        {
            Session.Add("allsettings", DB.getcurrentssettings());

            if (Session["allsettings"] != null)
            {

                DataRow drr = (DataRow)Session["allsettings"];
                DateTime lastlogin = DateTime.Parse(drr["logininout"].ToString());
                string format = "d/M/yyyy";
                lastlogins.Text += "<br/>  " + lastlogin.ToString(format) + "   " + lastlogin.ToShortTimeString();
                //lastlogins.Text += "<BR/> " + lastlogin.ToShortDateString(). + " " + lastlogin.ToShortTimeString();
                //////////////////////////////


                pop3Client.Connect();

                DataTable newmsgs = new DataTable();
                newmsgs.Columns.Add("msg");
                for (int i = 1; i < pop3Client.GetMessageCount() + 1; i++)
                {
                    DateTime dt = pop3Client.GetMessage(i).Headers.DateSent;
                    if (true)
                    {
                        newmails++;
                        newmsgs.Rows.Add( pop3Client.GetMessage(i).Headers.Subject );
                    }
                }
                if (newmails == 0)
                {
                    newmail.Text += newmails.ToString();
                    newmsgs.Rows.Add("لا يوجد رسائل الأن.اضفط للذهاب للبريد الوارد");
                    notificationsRepeater.DataSource = newmsgs;
                    Page.DataBind();
                }
                else
                {
                    newmail.Text += newmails.ToString();
                    notificationsRepeater.DataSource = newmsgs;
                    Page.DataBind();
                }


                //tomails.Text = newmail.Text;
                //DateTime myfirst = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
                //string first = myfirst.AddMonths(1).ToString("yyyy-MM-dd");
                //DataRow ddr = ((DataSet)DB.getdata("select count(id) from daily_checking where company_id in(select id from companies) and issuedate >'" + first + "'")).Tables[0].Rows[0];
                //if ((DateTime.Now.Day > 0) && (int.Parse(ddr[0].ToString()) == 0))
                //{
                //    updatesubs.Visible = true;
                //}
            }


        }

    }
    protected override void InitializeCulture()
    {
        if (Session["CurrentCulture"] != null)
        {
            string lang = Session["CurrentCulture"].ToString();
            if (lang != null && lang != "")
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
                Thread.CurrentThread.CurrentCulture.DateTimeFormat = new CultureInfo("en-US", false).DateTimeFormat;



            }
        }
    }

    protected void edit_Click(object sender, EventArgs e)
    {
        LinkButton lk = (LinkButton)sender; 
        string CompanyID =  lk.CommandArgument;
        Sendmail1.sets(lk.ToolTip, RoleEnum.Admin, RoleEnum.Organization, CompanyID, "");
        //cid = "Mails.aspx?mail=" + lk.ToolTip;
        //displays.Attributes["src"] = cid;
     
     
        Page.ClientScript.RegisterStartupScript(this.GetType(), "reports", "there();", true);

    }
    protected void mailsnot_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Inbox.aspx");
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        DB.MonthlyJob();
        //updatesubs.Visible = false;
        
    }
}
